//
//  JKRPublishViewController.m
//  百思不得姐
//
//  Created by Lucky on 16/3/9.
//  Copyright © 2016年 Lucky. All rights reserved.
//

#import "JKRPublishViewController.h"

static CGFloat const JKRAnimationDelay = 0.1;
static CGFloat const JKRSpringFactor = 10;

@interface JKRPublishViewController ()

@property (weak, nonatomic) IBOutlet UIButton *cancelButton;

@property (weak, nonatomic) UIImageView *sloganView;

@end

@implementation JKRPublishViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.cancelButton.layer.cornerRadius = 5;
    self.cancelButton.layer.masksToBounds = YES;
    self.cancelButton.layer.borderColor = [UIColor darkGrayColor].CGColor;
    self.cancelButton.layer.borderWidth = 1;
    
    // 数据
    NSArray *images = @[@"publish-video", @"publish-picture", @"publish-text", @"publish-audio", @"publish-review", @"publish-offline"];
    NSArray *titles = @[@"发视频", @"发图片", @"发段子", @"发声音", @"审帖", @"离线下载"];
    
    int maxCols = 3;
    CGFloat buttonW = 72;
    CGFloat buttonH = buttonW + 30;
    CGFloat buttonStartY = (JKRScreenH - 2 * buttonH) * 0.5;
    CGFloat buttonStartX = 20;
    CGFloat xMargin = (JKRScreenW - 2 * buttonStartX - maxCols * buttonW) / (maxCols - 1);
    
    for (int i = 0; i < images.count; i++) {
        JKRVerticalButton *button = [[JKRVerticalButton alloc] init];
        button.tag = i;
        [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:button];
        
        button.titleLabel.font = [UIFont systemFontOfSize:14];
        [button setTitle:titles[i] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [button setImage:[UIImage imageNamed:images[i]] forState:UIControlStateNormal];
        
//        button.width = buttonW;
//        button.height = buttonH;
        int row = i / maxCols;
        int col = i % maxCols;
//        button.x = buttonStartX + col * (xMargin + buttonW);
//        button.y = buttonStartY + row * buttonH;
        CGFloat buttonX = buttonStartX + col * (xMargin + buttonW);
        CGFloat buttonEndY = buttonStartY +row * buttonH;
        CGFloat buttonBeginY = buttonEndY - JKRScreenH;
        
        POPSpringAnimation *anim = [POPSpringAnimation animationWithPropertyNamed:kPOPViewFrame];
        anim.fromValue = [NSValue valueWithCGRect:CGRectMake(buttonX, buttonBeginY, buttonW, buttonH)];
        anim.toValue = [NSValue valueWithCGRect:CGRectMake(buttonX, buttonEndY, buttonW, buttonH)];
        anim.springBounciness = JKRSpringFactor;
        anim.springSpeed = JKRSpringFactor;
        anim.beginTime = CACurrentMediaTime() + JKRAnimationDelay * i;
        [button pop_addAnimation:anim forKey:nil];
    }
    
    UIImageView *sloganView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"app_slogan"]];
    [self.view addSubview:sloganView];
    _sloganView = sloganView;
    
    
    CGFloat centerX = JKRScreenW * 0.5;
    CGFloat centerEndY = JKRScreenH * 0.2;
    CGFloat centerBeginY = centerEndY - JKRScreenH;
    
    _sloganView.center = CGPointMake(centerX, centerBeginY);
    
    POPSpringAnimation *anim = [POPSpringAnimation animationWithPropertyNamed:kPOPViewCenter];
    anim.fromValue = [NSValue valueWithCGPoint:CGPointMake(centerX, centerBeginY)];
    anim.toValue = [NSValue valueWithCGPoint:CGPointMake(centerX, centerEndY)];
    anim.beginTime = CACurrentMediaTime() + images.count * JKRAnimationDelay;
    anim.springBounciness = JKRSpringFactor;
    anim.springSpeed = JKRSpringFactor;
    [anim setCompletionBlock:^(POPAnimation *anim, BOOL finished) {
        self.view.userInteractionEnabled = YES;
    }];
    [sloganView pop_addAnimation:anim forKey:nil];
}

- (IBAction)cancel
{
    [self cancelWithAnimationBlock:^{
        
    }];
}

- (void)buttonClick:(UIButton *)sender
{
    
}

- (void)cancelWithAnimationBlock:(void(^)())block
{
    self.view.userInteractionEnabled = NO;
    int beginIndex = 2;
    for (int i = beginIndex; i < self.view.subviews.count; i++) {
        UIView *subView = self.view.subviews[i];
        POPBasicAnimation *anim = [POPBasicAnimation animationWithPropertyNamed:kPOPViewCenter];
        CGFloat centerY = subView.centerY + JKRScreenH;
        anim.toValue = [NSValue valueWithCGPoint:CGPointMake(subView.centerX, centerY)];
        anim.beginTime = CACurrentMediaTime() + (i - beginIndex) * JKRAnimationDelay;
        [subView pop_addAnimation:anim forKey:nil];
        
        if (i == self.view.subviews.count - 1) {
            [anim setCompletionBlock:^(POPAnimation *anim, BOOL finished) {
                [self dismissViewControllerAnimated:NO completion:nil];
                
                !block ? : block();
            }];
        }
    }
}

@end
